package o;

import android.view.Surface;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Player.d;
import com.google.android.exoplayer2.metadata.Metadata;
import com.google.android.exoplayer2.source.TrackGroupArray;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;
import o.aca.b;
import org.checkerframework.checker.nullness.qual.RequiresNonNull;

public class abx implements d, ahh, acf, anz, ail, o.ald.e, adj, aoc, abw {
    private final o.abr.e ˊ;
    private final c ˋ;
    private final CopyOnWriteArraySet<aca> ˎ;
    private Player ˏ;
    private final ams ॱ;

    public static class a {
        public abx ˊ(@Nullable Player player, ams o_ams) {
            return new abx(player, o_ams);
        }
    }

    static final class c {
        private boolean ʻ;
        private abr ʽ = abr.ˊ;
        private final HashMap<o.aim.a, e> ˊ = new HashMap();
        @Nullable
        private e ˋ;
        private final o.abr.c ˎ = new o.abr.c();
        @Nullable
        private e ˏ;
        private final ArrayList<e> ॱ = new ArrayList();

        @Nullable
        public e ˎ() {
            if (this.ॱ.isEmpty() || this.ʽ.ˏ() || this.ʻ) {
                return null;
            }
            return (e) this.ॱ.get(0);
        }

        @Nullable
        public e ˏ() {
            return this.ˏ;
        }

        @Nullable
        public e ˋ() {
            return this.ˋ;
        }

        @Nullable
        public e ॱ() {
            if (this.ॱ.isEmpty()) {
                return null;
            }
            return (e) this.ॱ.get(this.ॱ.size() - 1);
        }

        @Nullable
        public e ˋ(o.aim.a aVar) {
            return (e) this.ˊ.get(aVar);
        }

        public boolean ˊ() {
            return this.ʻ;
        }

        @Nullable
        public e ˎ(int i) {
            int i2 = 0;
            e eVar = null;
            while (i2 < this.ॱ.size()) {
                e eVar2 = (e) this.ॱ.get(i2);
                int ˎ = this.ʽ.ˎ(eVar2.ˎ.ˋ);
                if (ˎ == -1 || this.ʽ.ˏ(ˎ, this.ˎ).ॱ != i) {
                    eVar2 = eVar;
                } else if (eVar != null) {
                    return null;
                }
                i2++;
                eVar = eVar2;
            }
            return eVar;
        }

        public void ॱ(int i) {
            ʻ();
        }

        public void ॱ(abr o_abr) {
            for (int i = 0; i < this.ॱ.size(); i++) {
                e ˎ = ˎ((e) this.ॱ.get(i), o_abr);
                this.ॱ.set(i, ˎ);
                this.ˊ.put(ˎ.ˎ, ˎ);
            }
            if (this.ˋ != null) {
                this.ˋ = ˎ(this.ˋ, o_abr);
            }
            this.ʽ = o_abr;
            ʻ();
        }

        public void ʽ() {
            this.ʻ = true;
        }

        public void ॱॱ() {
            this.ʻ = false;
            ʻ();
        }

        public void ˊ(int i, o.aim.a aVar) {
            e eVar = new e(aVar, (this.ʽ.ˎ(aVar.ˋ) != -1 ? 1 : null) != null ? this.ʽ : abr.ˊ, i);
            this.ॱ.add(eVar);
            this.ˊ.put(aVar, eVar);
            if (this.ॱ.size() == 1 && !this.ʽ.ˏ()) {
                ʻ();
            }
        }

        public boolean ˏ(o.aim.a aVar) {
            e eVar = (e) this.ˊ.remove(aVar);
            if (eVar == null) {
                return false;
            }
            this.ॱ.remove(eVar);
            if (this.ˋ != null && aVar.equals(this.ˋ.ˎ)) {
                this.ˋ = this.ॱ.isEmpty() ? null : (e) this.ॱ.get(0);
            }
            return true;
        }

        public void ˎ(o.aim.a aVar) {
            this.ˋ = (e) this.ˊ.get(aVar);
        }

        private void ʻ() {
            if (!this.ॱ.isEmpty()) {
                this.ˏ = (e) this.ॱ.get(0);
            }
        }

        private e ˎ(e eVar, abr o_abr) {
            int ˎ = o_abr.ˎ(eVar.ˎ.ˋ);
            if (ˎ == -1) {
                return eVar;
            }
            return new e(eVar.ˎ, o_abr, o_abr.ˏ(ˎ, this.ˎ).ॱ);
        }
    }

    static final class e {
        public final abr ˊ;
        public final int ˋ;
        public final o.aim.a ˎ;

        public e(o.aim.a aVar, abr o_abr, int i) {
            this.ˎ = aVar;
            this.ˊ = o_abr;
            this.ˋ = i;
        }
    }

    protected abx(@Nullable Player player, ams o_ams) {
        if (player != null) {
            this.ˏ = player;
        }
        this.ॱ = (ams) amm.ॱ(o_ams);
        this.ˎ = new CopyOnWriteArraySet();
        this.ˋ = new c();
        this.ˊ = new o.abr.e();
    }

    public final void ˎ() {
        if (!this.ˋ.ˊ()) {
            b ʼ = ʼ();
            this.ˋ.ʽ();
            Iterator it = this.ˎ.iterator();
            while (it.hasNext()) {
                ((aca) it.next()).ॱ(ʼ);
            }
        }
    }

    public final void ॱ() {
        for (e eVar : new ArrayList(this.ˋ.ॱ)) {
            ˏ(eVar.ˋ, eVar.ˎ);
        }
    }

    public final void ˋ(Metadata metadata) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˋ(ʼ, metadata);
        }
    }

    public final void ॱ(acz o_acz) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ʼ, 1, o_acz);
        }
    }

    public final void ॱ(String str, long j, long j2) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ˊॱ, 1, str, j2);
        }
    }

    public final void ˎ(Format format) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ˊॱ, 1, format);
        }
    }

    public final void ˏ(int i, long j, long j2) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˎ(ˊॱ, i, j, j2);
        }
    }

    public final void ˎ(acz o_acz) {
        b ʻ = ʻ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ʻ, 1, o_acz);
        }
    }

    public final void ॱ(int i) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˎ(ˊॱ, i);
        }
    }

    public void ˎ(float f) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˊ(ˊॱ, f);
        }
    }

    public final void ˏ(acz o_acz) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ʼ, 2, o_acz);
        }
    }

    public final void ˎ(String str, long j, long j2) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ˊॱ, 2, str, j2);
        }
    }

    public final void ˏ(Format format) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ˊॱ, 2, format);
        }
    }

    public final void ॱ(int i, long j) {
        b ʻ = ʻ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ʻ, i, j);
        }
    }

    public final void ˋ(acz o_acz) {
        b ʻ = ʻ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ʻ, 2, o_acz);
        }
    }

    public final void ˎ(@Nullable Surface surface) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˋ(ˊॱ, surface);
        }
    }

    public final void ˋ() {
    }

    public final void ˏ(int i, int i2, int i3, float f) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ˊॱ, i, i2, i3, f);
        }
    }

    public void ˎ(int i, int i2) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ˊॱ, i, i2);
        }
    }

    public final void ˊ(int i, o.aim.a aVar) {
        this.ˋ.ˊ(i, aVar);
        b ॱ = ॱ(i, aVar);
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˊ(ॱ);
        }
    }

    public final void ˏ(int i, o.aim.a aVar) {
        b ॱ = ॱ(i, aVar);
        if (this.ˋ.ˏ(aVar)) {
            Iterator it = this.ˎ.iterator();
            while (it.hasNext()) {
                ((aca) it.next()).ˋ(ॱ);
            }
        }
    }

    public final void ˏ(int i, @Nullable o.aim.a aVar, o.ail.a aVar2, o.ail.e eVar) {
        b ॱ = ॱ(i, aVar);
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ॱ, aVar2, eVar);
        }
    }

    public final void ˎ(int i, @Nullable o.aim.a aVar, o.ail.a aVar2, o.ail.e eVar) {
        b ॱ = ॱ(i, aVar);
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ॱ, aVar2, eVar);
        }
    }

    public final void ॱ(int i, @Nullable o.aim.a aVar, o.ail.a aVar2, o.ail.e eVar) {
        b ॱ = ॱ(i, aVar);
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˊ(ॱ, aVar2, eVar);
        }
    }

    public final void ˊ(int i, @Nullable o.aim.a aVar, o.ail.a aVar2, o.ail.e eVar, IOException iOException, boolean z) {
        b ॱ = ॱ(i, aVar);
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ॱ, aVar2, eVar, iOException, z);
        }
    }

    public final void ˎ(int i, o.aim.a aVar) {
        this.ˋ.ˎ(aVar);
        b ॱ = ॱ(i, aVar);
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ॱ);
        }
    }

    public final void ˊ(int i, @Nullable o.aim.a aVar, o.ail.e eVar) {
        b ॱ = ॱ(i, aVar);
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ॱ, eVar);
        }
    }

    public final void ॱ(abr o_abr, @Nullable Object obj, int i) {
        this.ˋ.ॱ(o_abr);
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ʼ, i);
        }
    }

    public final void ˊ(TrackGroupArray trackGroupArray, akx o_akx) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˊ(ʼ, trackGroupArray, o_akx);
        }
    }

    public final void ˎ(boolean z) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱ(ʼ, z);
        }
    }

    public final void ˏ(boolean z, int i) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ʼ, z, i);
        }
    }

    public final void ˋ(int i) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ʼ, i);
        }
    }

    public final void ˊ(ExoPlaybackException exoPlaybackException) {
        b ˋॱ;
        if (exoPlaybackException.ˎ == 0) {
            ˋॱ = ˋॱ();
        } else {
            ˋॱ = ʼ();
        }
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˊ(ˋॱ, exoPlaybackException);
        }
    }

    public final void ˎ(int i) {
        this.ˋ.ॱ(i);
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˋ(ʼ, i);
        }
    }

    public final void ˎ(abq o_abq) {
        b ʼ = ʼ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ʼ, o_abq);
        }
    }

    public final void ˊ() {
        if (this.ˋ.ˊ()) {
            this.ˋ.ॱॱ();
            b ʼ = ʼ();
            Iterator it = this.ˎ.iterator();
            while (it.hasNext()) {
                ((aca) it.next()).ˎ(ʼ);
            }
        }
    }

    public final void ˋ(int i, long j, long j2) {
        b ˋॱ = ˋॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˊ(ˋॱ, i, j, j2);
        }
    }

    public final void ˏ() {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ॱॱ(ˊॱ);
        }
    }

    public final void ᐝ() {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ʻ(ˊॱ);
        }
    }

    public final void ˎ(Exception exception) {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ˏ(ˊॱ, exception);
        }
    }

    public final void ॱॱ() {
        b ˊॱ = ˊॱ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ᐝ(ˊॱ);
        }
    }

    public final void ʽ() {
        b ʻ = ʻ();
        Iterator it = this.ˎ.iterator();
        while (it.hasNext()) {
            ((aca) it.next()).ʽ(ʻ);
        }
    }

    @RequiresNonNull({"player"})
    protected b ˋ(abr o_abr, int i, @Nullable o.aim.a aVar) {
        o.aim.a aVar2;
        long j = 0;
        Object obj = 1;
        if (o_abr.ˏ()) {
            aVar2 = null;
        } else {
            aVar2 = aVar;
        }
        long ˏ = this.ॱ.ˏ();
        Object obj2 = (o_abr == this.ˏ.ᐝॱ() && i == this.ˏ.ॱॱ()) ? 1 : null;
        if (aVar2 != null && aVar2.ˊ()) {
            if (!(obj2 != null && this.ˏ.ͺ() == aVar2.ॱ && this.ˏ.ˋॱ() == aVar2.ˊ)) {
                obj = null;
            }
            if (obj != null) {
                j = this.ˏ.ʼ();
            }
        } else if (obj2 != null) {
            j = this.ˏ.ॱˎ();
        } else if (!o_abr.ˏ()) {
            j = o_abr.ˎ(i, this.ˊ).ˋ();
        }
        return new b(ˏ, o_abr, i, aVar2, j, this.ˏ.ʼ(), this.ˏ.ˊॱ());
    }

    private b ˎ(@Nullable e eVar) {
        amm.ॱ(this.ˏ);
        if (eVar == null) {
            int ॱॱ = this.ˏ.ॱॱ();
            eVar = this.ˋ.ˎ(ॱॱ);
            if (eVar == null) {
                abr ᐝॱ = this.ˏ.ᐝॱ();
                if ((ॱॱ < ᐝॱ.ॱ() ? 1 : null) == null) {
                    ᐝॱ = abr.ˊ;
                }
                return ˋ(ᐝॱ, ॱॱ, null);
            }
        }
        return ˋ(eVar.ˊ, eVar.ˋ, eVar.ˎ);
    }

    private b ʻ() {
        return ˎ(this.ˋ.ˏ());
    }

    private b ʼ() {
        return ˎ(this.ˋ.ˎ());
    }

    private b ˊॱ() {
        return ˎ(this.ˋ.ˋ());
    }

    private b ˋॱ() {
        return ˎ(this.ˋ.ॱ());
    }

    private b ॱ(int i, @Nullable o.aim.a aVar) {
        amm.ॱ(this.ˏ);
        if (aVar != null) {
            e ˋ = this.ˋ.ˋ(aVar);
            if (ˋ != null) {
                return ˎ(ˋ);
            }
            return ˋ(abr.ˊ, i, aVar);
        }
        abr ᐝॱ = this.ˏ.ᐝॱ();
        if ((i < ᐝॱ.ॱ() ? 1 : null) == null) {
            ᐝॱ = abr.ˊ;
        }
        return ˋ(ᐝॱ, i, null);
    }
}
